var fs = require("fs");
/**基本组有哪些,每个组有多少个皮肤，有用到哪些图片资源*/
/**type:pc or mobile*/
exports.readSkins = function (type) {
    var config = JSON.parse(fs.readFileSync("./config/groupConfig.json"))[type];
    var groupBasic = [];

    for(var groupName in config) {
        groupBasic[groupName] = {};
        findSkins(groupBasic,type,config[groupName],groupName);
    }

    var obj = {};
    for(var a in groupBasic ){
        obj[a] = groupBasic[a].sources;
    }
    return obj;
}

/**解析某个基本组下的皮肤*/
function findSkins(groupBasic,type,paths, groupName) {
    groupBasic[groupName].skinCount = paths.length;
    groupBasic[groupName].readedSkinCount = 0;
    groupBasic[groupName].sources = [];

    for(var i=0; i<paths.length; i++) {
        readSkin(groupBasic,type,paths[i],groupName);
    }
}
/**提取一个皮肤文件中的 _png _jpg*/
function readSkin(groupBasic,type,path,groupName) {
    var skinFile = fs.readFileSync(path);
    var fileStr = skinFile.toString();
    var arr = fileStr.split('"');
    for(var j=0;j<arr.length;j++) {
        if( (arr[j].indexOf("_png")!=-1 || arr[j].indexOf("_jpg")!=-1) && groupBasic[groupName].sources.indexOf(arr[j])== -1){
            groupBasic[groupName].sources.push(arr[j]);
        }
    }
    groupBasic[groupName].readedSkinCount ++;

    if(groupBasic[groupName].readedSkinCount == groupBasic[groupName].skinCount){
        readSkinsComplete(groupBasic,groupName,type);
    }
}
/**一个基本组解析完成,对这个组的结果做筛选*/
function readSkinsComplete(groupBasic,groupName,type){
    // 把代码中的资源读入配置中
    var codeFile = fs.readFileSync("./config/codeResConfig.json");
    var views = JSON.parse(codeFile.toString())[type];
    if(views[groupName].length > 0) {
        groupBasic[groupName].sources = groupBasic[groupName].sources.concat(views[groupName]);
    }

    //去除default中已有组的资源
    var codeFile = fs.readFileSync("./config/defaultGroup.json");
    var defaultGroups = JSON.parse(codeFile.toString()).defaultGroups;
    var defaultSources = [];
    for(var i=0;i<defaultGroups.length;i++){
        defaultSources = defaultSources.concat(defaultGroups[i].keys.split(","));
    }
    for(var j=0;j<defaultSources.length;j++) {
        var index = groupBasic[groupName].sources.indexOf(defaultSources[j]);
        if(index!=-1) {
            console.log("去除默认组里已有的资源 "+defaultSources[j]);
            groupBasic[groupName].sources.splice(index,1);
        }
    }


    var langFile = fs.readFileSync("./config/language.json");
    var lang = JSON.parse(langFile.toString());
    var frames = lang.frames;
    for(var name in frames) {
        var index = groupBasic[groupName].sources.indexOf(name);
        if(index!=-1) {
            console.log("去除多语言资源 "+name);
            groupBasic[groupName].sources.splice(index,1);
        }
        var index = groupBasic[groupName].sources.indexOf("language_json."+name);
        if(index!=-1) {
            console.log("去除languege_json. 格式的多语言资源 "+name);
            groupBasic[groupName].sources.splice(index,1);
        }
    }


    var langFile1 = fs.readFileSync("./config/pc_language.json");
    var lang1 = JSON.parse(langFile1.toString());
    var frames1 = lang1.frames;
    for(var name in frames1) {
        var index = groupBasic[groupName].sources.indexOf(name);
        if(index!=-1) {
            console.log("去除多语言资源 "+name);
            groupBasic[groupName].sources.splice(index,1);
        }
        var index = groupBasic[groupName].sources.indexOf("language_json."+name);
        if(index!=-1) {
            console.log("去除languege_json. 格式的多语言资源 "+name);
            groupBasic[groupName].sources.splice(index,1);
        }

        index = groupBasic[groupName].sources.indexOf("pc_language_json."+name);
        if(index!=-1) {
            console.log("去除pc_languege_json. 格式的多语言资源 "+name);
            groupBasic[groupName].sources.splice(index,1);
        }
    }

    for(var i=0;i<groupBasic[groupName].sources.length;i++) {
        if(groupBasic[groupName].sources[i].indexOf("poker_") != -1 || groupBasic[groupName].sources[i].indexOf("poker_") != -1 ) {
            console.log("去除poker资源 "+groupBasic[groupName].sources[i]);
            groupBasic[groupName].sources.splice(i--,1);
        }
    }

    console.log("剩下的sources:"+groupBasic[groupName].sources.length);
}
